﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;


namespace Kooboo.Web.Areas.Commerce.Web.UI
{

    using System.Web.Script.Serialization;
    using Kooboo.Commerce.AddIn;

    using Kooboo.IoC;
    using Kooboo.Commerce;
    using Kooboo.Commerce.Contexts.Categories;
    using Kooboo.Commerce.Contexts.Brands;
    using Kooboo.Commerce.Contexts.ProductTypes;
    using Kooboo.Commerce.Contexts.Countries;

    public static class Selector
    {

        #region Category
        [Obsolete]
        public static MvcHtmlString SelectorFor<T>(this HtmlHelper htmlHelper, string name, object htmlAttributes = null) where T : ICategory
        {
            var host = new ServiceHost();

            var list = host.QueryCategories().Where(i => i.Parent == null).OrderBy(i => i.Name);

            List<SelectListItem> items = new List<SelectListItem>();
            items.Add(new SelectListItem() { Value = "" });

            var selecteds = new List<ICategory>();

            foreach (var i in list)
            {
                items.AddRange(BuildSelectListItems(i, selecteds, "·"));
            }

            return htmlHelper.DropDownList(name, items, htmlAttributes);
        }

        [Obsolete]
        public static MvcHtmlString SelectorFor<TProperty>(this HtmlHelper<ICategory> htmlHelper, Expression<Func<ICategory, TProperty>> expression)
        {
            var host = new ServiceHost();

            var list = host.QueryCategories().Where(i => i.Parent == null).OrderBy(i => i.Name);

            List<SelectListItem> items = new List<SelectListItem>();
            items.Add(new SelectListItem() { Value = "" });

            var selecteds = new List<ICategory>();
            if (htmlHelper.ViewData.Model.Parent != null)
            {
                selecteds.Add(htmlHelper.ViewData.Model.Parent);
            }

            foreach (var i in list)
            {
                items.AddRange(BuildSelectListItems(i, selecteds, "·"));
            }

            return htmlHelper.DropDownListFor(expression, items);
        }
        [Obsolete]
        public static MvcHtmlString MultiSelectorFor(this HtmlHelper<IProduct> htmlHelper, Expression<Func<IProduct, IQueryable<ICategory>>> expression)
        {
            return MultiSelectorFor(htmlHelper, "CategoryList", htmlHelper.ViewData.Model.Categories.ToList());
        }
        [Obsolete]
        public static List<SelectListItem> BuildCategories(IEnumerable<ICategory> selecteds)
        {
            var host = new ServiceHost();

            var list = host
                .QueryCategories()
                .Where(i => i.Parent == null)
                .OrderBy(i => i.Name);

            List<SelectListItem> items = new List<SelectListItem>();


            foreach (var i in list)
            {
                items.AddRange(BuildSelectListItems(i, selecteds, "·"));
            }

            return items;
        }
        [Obsolete]
        public static MvcHtmlString MultiSelectorFor(this HtmlHelper htmlHelper, string name, IEnumerable<ICategory> selecteds)
        {
            var host = new ServiceHost();
            var list = host
                .QueryCategories()
                .Where(i => i.Parent == null)
                .OrderBy(i => i.Name);

            List<SelectListItem> items = new List<SelectListItem>();


            foreach (var i in list)
            {
                items.AddRange(BuildSelectListItems(i, selecteds, "·"));
            }


            var html = htmlHelper.DropDownList(name, items, new { multiple = "multiple" });

            var serializer = new JavaScriptSerializer();

            var script = "<script type=text/javascript>$(document).ready(function(){$('#CategoryList').dropdownchecklist(); })</script>";

            return MvcHtmlString.Create(html.ToHtmlString() + script);
        }

        static List<SelectListItem> BuildSelectListItems(ICategory item, IEnumerable<ICategory> selecteds, string indent)
        {
            List<SelectListItem> items = new List<SelectListItem>();
            items.Add(new SelectListItem() { Text = indent + item.Name, Value = item.Id.ToString(), Selected = selecteds == null ? false : selecteds.Any(i => i.Id == item.Id) });

            foreach (var i in item.Children.OrderBy(i => i.Name))
            {
                items.AddRange(BuildSelectListItems(i, selecteds, indent + "·"));
            }


            return items;
        }


        #endregion

        #region Brands
        [Obsolete]
        public static MvcHtmlString SelectorFor(this HtmlHelper html, string name, IBrand selected, object htmlAttributes = null, bool allowNull = false)
        {
            var host = new ServiceHost();
            var list = host
                .QueryBrands()
                .OrderBy(i => i.Name).ToList();

            var selectedId = selected == null ? -1 : selected.Id;

            List<SelectListItem> items = new List<SelectListItem>();


            foreach (var i in list)
            {
                items.Add(new SelectListItem() { Text = i.Name, Value = i.Id.ToString(), Selected = i.Id == selectedId });

            }

            if (allowNull)
            {
                items.Insert(0, new SelectListItem { Text = "", Value = "" });
            }

            return html.DropDownList(name, items, htmlAttributes);
        }

        #endregion

        #region ValidationRule
        [Obsolete]
        public static MvcHtmlString Validators(this HtmlHelper htmlHelper, string name)
        {

            var list = ObjectContainer.CreateInstances<IValidator>();

            var items = list
                .Select(i => new SelectListItem
                {
                    Text = i.Name,
                    Value = i.Name
                });


            var html = htmlHelper.DropDownList(name, items);

            var serializer = new JavaScriptSerializer();

            var script = "<script type=text/javascript>$.fn.validationrule.defaults.validators = [";

            var validators = new List<string>();
            foreach (var i in list)
            {
                validators.Add(serializer.Serialize(new { Name = i.Name, Parameters = i.GetParameters() }));
            }

            script += string.Join(", ", validators.ToArray());

            script += "];</script>";

            return MvcHtmlString.Create(html.ToHtmlString() + script);
        }



        #endregion

        #region Promotions
        [Obsolete]
        public static MvcHtmlString SelectorFor(this HtmlHelper<IPromotionRule> html, Expression<Func<IPromotionRule, string>> expression, object htmlAttributes = null)
        {
            var promotions = ObjectContainer.CreateInstances<IPromotion>()
            .Select(i => new { i.Name, i.HeadLine });

            return html.DropDownListFor(expression, new SelectList(promotions, "Name", "HeadLine"), htmlAttributes);
        }

        #endregion

        #region  ProductType
        [Obsolete]
        public static MvcHtmlString SelectorFor(this HtmlHelper html, string name, IProductType selected, object htmlAttributes = null, bool allowNull = false)
        {
            var host = new ServiceHost();

            var list = host
                .QueryProductTypes()
                .Where(i => i.IsEnable)
                .OrderBy(i => i.Name)
                .ToList();

            var items = new SelectList(list, "Id", "Name", selected).ToList();

            if (allowNull)
            {
                items.Insert(0, new SelectListItem { Value = "", Text = "" });
            }


            return html.DropDownList(name, items, htmlAttributes);
        }

        static MvcHtmlString MultiSelectorFor(this HtmlHelper htmlHelper, string name, IEnumerable<IProductType> selecteds)
        {
            var host = new ServiceHost();
            var list = host
                .QueryProductTypes()
                .Where(i => i.IsEnable)
                .OrderBy(i => i.Name);

            List<SelectListItem> items = new List<SelectListItem>();


            foreach (var i in list)
            {
                var item = new SelectListItem()
                {
                    Value = i.Id.ToString(),
                    Text = i.Name,
                    Selected = selecteds.Any(n => n.Id == i.Id)
                };

                items.Add(item);
                //items.AddRange(BuildSelectListItems(i, selecteds, "·"));
            }


            var html = htmlHelper.DropDownList(name, items, new { multiple = "multiple" });

            var serializer = new JavaScriptSerializer();

            var script = "<script type=text/javascript>$(document).ready(function(){$('#" + name + "').dropdownchecklist(); })</script>";

            return MvcHtmlString.Create(html.ToHtmlString() + script);
        }

        #endregion

        #region SupportStatus
        [Obsolete]
        public static MvcHtmlString SelectorFor(this HtmlHelper<IQueryable<ISupportTicket>> htmlHelper, string name, SupportStatus value, object htmlAttributes = null)
        {
            var values = from SupportStatus e in Enum.GetValues(typeof(SupportStatus))
                         select new
                         {
                             Value = (int)e,
                             Text = e.ToString()
                         };

            var selectList = new SelectList(values, "Value", "Text", new { Value = (int)value, Text = value.ToString() });

            return htmlHelper.DropDownList(name, selectList, htmlAttributes);
        }
        #endregion

        #region Countries
        [Obsolete]
        public static MvcHtmlString SelectorFor(this HtmlHelper html, string name, ICountry selected, bool allowNull = true)
        {
            var host = new ServiceHost();
            var selectedCode = selected == null ? "" : selected.CountryCode;

            var list = host
                .QueryCountries()
                .OrderBy(i => i.CountryName)
                .Select(i => new SelectListItem
                {
                    Value = i.CountryCode,
                    Text = i.CountryName,
                    Selected = selectedCode == i.CountryCode
                })
                .ToList();

            if (allowNull)
            {
                list.Insert(0, new SelectListItem() { Text = "", Value = "" });
            }

            return html.DropDownList(name, list);
        }
        #endregion
    }
}